package ink.sunflowerk.util;

import javax.servlet.http.HttpServletRequest;

/**
 * @author zhaokuii11@163.com
 * @create 2021-12-01 20:59
 * @Description 判断请求类型、ajax还是 普通的
 */
public class CrowdUtil {
    /**
     * 判断当前请求是 ajax还是 普通请求
     * 判断依据：
     * Accept : application/json; charset=utf-8
     * X-requested-with: XMLHttpRequest
     * 只要有一个就可以
     *
     * @param request 请求对象
     * @return Boolean true ajax请求、false 普通请求
     */
    public static boolean judgeRequestType(HttpServletRequest request) {
        //1. 获取请求消息头
        String accept = request.getHeader("Accept");
        String xRequestedwith = request.getHeader("X-requested-with");
        //2. 判断
        if (accept != null && accept.contains("application/json") ||
                xRequestedwith != null && xRequestedwith.contains("XMLHttpRequest")) {
            return true;
        }
        return false;
    }
}